import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入登录组件
import Login from '../components/commen/login/login'
// 导入home相关的组件
import Home from '../views/home/Home'
import Welcome from '@/views/home/homechild/Welcome'
import User from '@/components/content/user/User'

// 导入权限列表power相关的组件
import Rights from '@/views/power/Rights'
import Roles from '@/views/power/Roles'

// 导入商品管理相关组件
import cate from '@/views/goods/cate'
import params from '@/views/goods/params'
import list from '@/views/goods/list'
import addgoods from '@/views/goods/addgoods'

// 导入订单管理组件
import Order from '@/views/order/order'

// 导入数据统计组件
import report from '@/views/report/report'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      // 将相关组件作为Home的子路由进行跳转
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: User },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: cate },
        { path: '/params', component: params },
        { path: '/goods', component: list },
        { path: '/goods/add', component: addgoods },
        { path: '/orders', component: Order },
        { path: '/reports', component: report }

      ]
    }
  ]
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to代表要访问的路径、from表示从哪个路径跳转而来
  // next是一个函数，表示放行，有两种表达方式：next()-->表示直接放行、next('/home')-->表示强制跳转到某个页面
  if (to.path === '/login') return next()
  // 获取token
  const tokens = window.sessionStorage.getItem('token')
  if (!tokens) return next('/login')
  next()
})
export default router
